#ifndef SPSCALEADAPTOR_H
#define SPSCALEADAPTOR_H

#include "iscaleadaptor.h"
#include <QSerialPort>
#include <QTimer>
#include <QLocale>
#include <QStringList>

class SpScaleAdaptor:public IScaleAdaptor
{
    Q_OBJECT
public:
    struct CmdHandler{
        const char* cmd;
        bool (SpScaleAdaptor::*handle)(QByteArray &array);

    };
    static SpScaleAdaptor* getInstance();
    bool handleInput(QByteArray& array);
    bool handleScaleStatus(QByteArray& array);
    bool handleWeight(QByteArray& array);//weight
    bool handle4Gcmd(QByteArray& array);


    static const char* WEIGHING_UNIT;
    static const char* WEIGHING_DECI;
    static const char* WEIGHING_MAX;
    static const char* WEIGHING_DIVI;
    //static const char* WEIGHING_ZERO;
    static const char* WEIGHING_HZERO;
    static const char* WEIGHING_AUTOZEROTRACE;
    static const char* WEIGHING_AUTOZERORANGE;

    static const char* WEIGHING_ADW;

//    static const char* CALI_AUTOZEROTRACE_SW;
//    static const char* CALI_AUTOZERORANGE;
//    static const char* CALI_BOOTAUTOZERO_SW;
//    static const char* CALI_BOOTAUTORANGE;
public slots:
    void syncScalePara();
private:
    SpScaleAdaptor(const QString& portName,QObject *parent = 0);
    static SpScaleAdaptor* mSsp;
    QSerialPort* mCom;
    const static CmdHandler cmdTable[];
    const static QStringList unitList;
    const static char* unitTab[];
    const static int expTab[];    
    const static QStringList diviList;
    const static QStringList deciList;
    //const static QStringList zeroList;
    //const static QStringList hzeroList;
    const static char* hzeroTab[];
    const static char* autoZTTab[];
    const static char* autoSZRTab[];

    bool handleReciveData(QByteArray &data);
    QTimer* mErrTimer;
    QString mStrNet;
    QString mStrGross;
    QString mStrTare;
    float mTare_g;
    bool mStab;
    bool mOverLoad;
    QByteArray mNotLineData;
signals:
    void inputCome(int num,int val);
    void setSuccess();
    void updateScale();//更新重量
    void getTare(const QString&);
    void updateDeci();

    // IScaleAdaptor interface
public:
    void writeCmd(const QString &);
    void setScale(const QString&,const QString&);
    void tareScale(float weight = 0);
    void zeroScale();
    //void setZero(const QString &);
    int getInput(int num);
    int setOutput(int num, int val);

    //void setUnit(const QString& data);
    QString getUnit();
    int getUnitIndex();
    //void setMax(const QString& data);
    float getMax();
    QString getMaxStr();
    QString getMaxkgStr();
    //void setDeci(const QString &data);
    int getDeci();
    //void setDivi(const QString &data);
    int getDivi();
    QString getDiviStr();
    int getDiviIndex();
    //void setZero(const QString &data);
    //int getZero();//多少个d;
    int getHZero();
    QString getHZeroStr();
    int getAutoZeroTrace();//零点追踪
    QString getAutoZeroTraceStr();
    int getAutoZeroRange();//置零范围
    QString getAutoZeroRangeStr();
//    void setAutoZeroTrace(const QString& data);
//    void setAutoZeroRange(const QString& data);
    QStringList getUnitList1();
    QStringList getUnitList() {return unitList;}
    QStringList getDeciList() {return deciList;}
    QStringList getDiviList() {return diviList;}
    //QStringList getZeroList() {return zeroList;}
    QStringList getHZeroList();
    QStringList getAutoZTList();
    QStringList getAutoSZRList();

    QString stringNet(){return mStrNet;}
    QString stringGross(){return mStrGross;}
    QString stringTare(){return QString::number(floatTare());}
    //QString stringWeight(float);
    float getAdw();
    bool setAdw(float w);
    float floatFdnWgt();
    float floatNet(){return mStrNet.toFloat();}
    float floatGross(){return mStrGross.toFloat();}
    float floatTare();
    void floatTare_g(const QString&,float*);
    QString floattoString(float f){return QLocale(QLocale::English).toString(f);}

    bool stabFlag();
    bool tareFlag();
    bool zeroFlag();
    bool overFlag() {return mOverLoad;}
protected slots:
    void onRead();

    void onErrTimeOut();
};

#endif // SPSCALEADAPTOR_H
